package com.py.payment.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.py.payment.dto.param.PayRecordsParamDto;
import com.py.payment.dto.ret.InfoRetDto;
import com.py.payment.dto.ret.LineChartRetDto;
import com.py.payment.dto.ret.RetDto;
import com.py.payment.entity.PyInfo;
import com.py.payment.entity.PyPayRecords;
import com.py.payment.service.PyInfoService;
import com.py.payment.service.PyPayRecordsService;
import com.py.payment.tools.ToolsUtils;
import com.py.payment.tools.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/info")
@CrossOrigin(origins = "*")
public class InfoController {

    @Autowired
    private PyInfoService infoService;

    @Autowired
    private PyPayRecordsService payRecordsService;

    @PostMapping(name = "获取基础信息列表",path = "/getInfoList")
    public ResponseData getInfoList(@RequestBody PyInfo info){
        QueryWrapper<PyInfo> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.eq("type",info.getType());
        List<InfoRetDto> list = this.infoService.getInfoList(info);
        return ResponseData.success(list);
    }

    @PostMapping(name = "支付台账折线图",path = "/getLineChart")
    public ResponseData getLineChart(@RequestBody PyInfo pyInfo){
        LineChartRetDto lineChart = this.payRecordsService.getLineChart(pyInfo);
        QueryWrapper<PyPayRecords> payRecordsQueryWrapper = new QueryWrapper<>();
        payRecordsQueryWrapper.select("IFNULL( sum( accounts_payable ), 0 ) accountsPayable,IFNULL( sum( net_payment ), 0 ) netPayment ");
        payRecordsQueryWrapper.eq("type",pyInfo.getType());
        if(ToolsUtils.isNotEmpty(pyInfo.getParentId())){
            QueryWrapper<PyInfo> infoQueryWrapper = new QueryWrapper<>();
            infoQueryWrapper.eq("parent_id",pyInfo.getParentId());
            infoQueryWrapper.eq("type",pyInfo.getType());
            List<PyInfo> list = this.infoService.list(infoQueryWrapper);
            List<Integer> idList = list.stream().map(PyInfo::getId).toList();
            payRecordsQueryWrapper.in("info_id",idList);
        }
        PyPayRecords records = this.payRecordsService.getOne(payRecordsQueryWrapper);

        BigDecimal accountsPayable = new BigDecimal(records.getAccountsPayable());
        BigDecimal netPayment = new BigDecimal(records.getNetPayment());
        BigDecimal deduction = accountsPayable.subtract(netPayment);

        JSONObject json = new JSONObject();
        json.put("lineChart",lineChart);
        json.put("accountsPayable",accountsPayable.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        json.put("netPayment",netPayment.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        json.put("deduction",deduction.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        json.put("cumulativeHave","0");
        return ResponseData.success(json);
    }

    /**
     * 建安工程
     * @return
     */
    @PostMapping(name = "建安工程",path = "/projectList")
    private ResponseData projectList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getProjectInfo(pyInfo);
        return ResponseData.success(retDto);
    }

    /**
     * 机电设备费
     * @return
     */
    @PostMapping(name = "机电设备费",path = "/electromechanicalList")
    public ResponseData electromechanicalList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        return ResponseData.success(retDto);
    }

    /**
     * 工程建设监理费
     * @return
     */
    @PostMapping(name = "工程建设监理费",path = "/supervisionList")
    public ResponseData supervisionList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getAllAmount(pyInfo);
        List<String> payTitleList = retDto.getPayTitleList();
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            payTitleList.forEach(i->{
                item.remove(i+"C");
            });
        });
        retDto.setInfoList(infoList);
        return ResponseData.success(retDto);
    }

    /**
     * 可研勘测设计费
     * @return
     */
    @PostMapping(name = "可研勘测设计费",path = "/deviseList")
    public ResponseData deviseList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
        });
        return ResponseData.success(retDto);
    }

    /**
     * 征地拆迁
     * @return
     */
    @PostMapping(name = "征地拆迁",path = "/demolitionList")
    public ResponseData demolitionList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
        });
        return ResponseData.success(retDto);
    }

    /**
     * 水土保持工程
     * @return
     */
    @PostMapping(name = "水土保持工程",path = "/soilList")
    public ResponseData soilList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
        });
        return ResponseData.success(retDto);
    }

    /**
     * 环境保护工程
     * @return
     */
    @PostMapping(name = "环境保护工程",path = "/environmentList")
    public ResponseData environmentList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
        });
        return ResponseData.success(retDto);
    }

    /**
     * 工程保险
     * @return
     */
    @PostMapping(name = "工程保险",path = "/insuranceList")
    public ResponseData insuranceList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
        });
        return ResponseData.success(retDto);
    }

    /**
     * 建设管理费
     * @return
     */
    @PostMapping(name = "建设管理费",path = "/constructionList")
    public ResponseData constructionList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
        });
        return ResponseData.success(retDto);
    }

    /**
     * 生产准备费
     * @return
     */
    @PostMapping(name = "生产准备费",path = "/prepareList")
    public ResponseData prepareList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
        });
        return ResponseData.success(retDto);
    }

    /**
     * 建设期利息
     * @param pyInfo
     * @return
     */
    @PostMapping(name = "建设期利息",path = "/interestList")
    public ResponseData interestList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.getOneAmount(pyInfo);
        List<JSONObject> infoList = retDto.getInfoList();
        infoList.forEach(item->{
            item.remove("tenderExtimation");
            item.remove("contractPrice");
        });
        retDto.setInfoList(infoList);
        return ResponseData.success(retDto);
    }

    /**
     * 支付汇总
     * @return
     */
    @PostMapping(name = "支付汇总",path = "/payList")
    public ResponseData payList(@RequestBody PyInfo pyInfo){
        RetDto retDto = this.infoService.payList(pyInfo);
        return ResponseData.success(retDto);
    }


    @PostMapping(name = "编辑支付台账",path = "updatePayMent")
    public ResponseData updatePayMent(@RequestBody PayRecordsParamDto param){
        PyPayRecords payRecords = new PyPayRecords();
        switch (param.getField().toUpperCase()) {
            case "A":
                payRecords.setAccountsPayable(param.getValue());
                break;
            case "B":
                payRecords.setNetPayment(param.getValue());
                break;
            case "C":
                payRecords.setRemarks(param.getValue());
                break;
            default:
                break;
        }
        QueryWrapper<PyPayRecords> payRecordsQueryWrapper = new QueryWrapper<>();
        payRecordsQueryWrapper.eq("info_id", param.getInfoId());
        payRecordsQueryWrapper.eq("title", param.getTitle());
        this.payRecordsService.update(payRecords,payRecordsQueryWrapper);
        return ResponseData.success();
    }

}


